using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.UIElements.Cursor;

namespace DA.AnimGraph.Editor {
    public static class AGEStyleUtility {
        public static StyleColor Color(int r, int g, int b, int a = 255) {
            return new Color(r / 255f, g / 255f, b / 255f, a / 255);
        }

        public static Cursor LoadCursor(MouseCursor cursorStyle) {
            object boxed = new Cursor();
            typeof(Cursor).GetProperty("defaultCursorId", BindingFlags.NonPublic | BindingFlags.Instance)
                ?.SetValue(boxed, (int)cursorStyle, null);
            return (Cursor)boxed;
        }

        public static void SetSubFieldLeftPadding(this VisualElement self, int level = 1) {
            self.style.paddingLeft = 15 * level;
        }

        public static void SetListElementStyle(this FoldoutPro self) {
            var s = self.style;

            float borderWidth = 1;
            s.borderLeftWidth = borderWidth;
            s.borderRightWidth = borderWidth;
            s.borderBottomWidth = borderWidth;

            var borderColor = Color(26, 26, 26);
            s.borderLeftColor = borderColor;
            s.borderRightColor = borderColor;
            s.borderTopColor = borderColor;
            s.borderBottomColor = borderColor;

            var titleBar = self.Q<Toggle>();
            var st = titleBar.style;
            st.marginTop = 0;
            st.marginBottom = 0;
            st.marginLeft = 0;
            st.marginRight = 0;

            var titleColor = Color(26, 26, 26);
            st.backgroundColor = titleColor;

            var cont = self.Q("unity-content");
            var sc = cont.style;

            sc.backgroundColor = Color(66, 66, 66);
            sc.borderLeftWidth = 15;
            sc.marginLeft = 0;
            sc.borderTopWidth = 2;
            sc.borderBottomWidth = 2;
        }
        public static void SetPerkListElementStyle(this FoldoutPro self) {
            var s = self.style;

            float borderWidth = 1;
            s.paddingBottom = 1;
            s.borderTopWidth = borderWidth;

            var borderColor = Color(26, 26, 26);
            s.borderTopColor = borderColor;
            s.borderBottomColor = borderColor;

            var titleBar = self.Q<Toggle>();
            var st = titleBar.style;
            st.marginTop = 0;
            st.marginBottom = 0;
            st.marginLeft = 0;
            st.marginRight = 0;

            var titleColor = Color(75, 75, 75);
            st.backgroundColor = titleColor;
        }
        public static VisualElement SetBorderWidth(this VisualElement ele, int width) {
            ele.style.borderBottomWidth = width;
            ele.style.borderLeftWidth = width;
            ele.style.borderRightWidth = width;
            ele.style.borderTopWidth = width;
            return ele;
        }
        public static VisualElement SetBorderColor(this VisualElement ele, StyleColor color) {
            ele.style.borderBottomColor = color;
            ele.style.borderLeftColor = color;
            ele.style.borderRightColor = color;
            ele.style.borderTopColor = color;
            return ele;
        }
    }
}
